package com.example.grade;

import java.util.ArrayList;
import java.util.List;

public class GradeManager {
    private List<StudentGrade> grades;

    public GradeManager() {
        this.grades = new ArrayList<>();
    }

    public void addGrade(String studentName, String studentId, String courseName, int grade) {
        if (grade < 0 || grade > 100) {
            System.out.println("成绩范围错误，必须在0到100之间！");
            return;
        }
        for (StudentGrade sg : grades) {
            if (sg.getStudentId().equals(studentId) && sg.getCourseName().equals(courseName)) {
                System.out.println("该学号和课程的成绩已存在！");
                return;
            }
        }
        grades.add(new StudentGrade(studentName, studentId, courseName, grade));
        System.out.println("成绩记录成功！");
    }

    public void queryGrade(String queryType, String queryValue) {
        boolean found = false;
        for (StudentGrade grade : grades) {
            switch (queryType) {
                case "姓名":
                    if (grade.getStudentName().equals(queryValue)) {
                        System.out.println("姓名：" + grade.getStudentName() + ", 学号：" + grade.getStudentId() +
                                ", 课程：" + grade.getCourseName() + ", 成绩：" + grade.getGrade());
                        found = true;
                    }
                    break;
                case "学号":
                    if (grade.getStudentId().equals(queryValue)) {
                        System.out.println("姓名：" + grade.getStudentName() + ", 学号：" + grade.getStudentId() +
                                ", 课程：" + grade.getCourseName() + ", 成绩：" + grade.getGrade());
                        found = true;
                    }
                    break;
                case "课程":
                    if (grade.getCourseName().equals(queryValue)) {
                        System.out.println("姓名：" + grade.getStudentName() + ", 学号：" + grade.getStudentId() +
                                ", 课程：" + grade.getCourseName() + ", 成绩：" + grade.getGrade());
                        found = true;
                    }
                    break;
            }
        }
        if (!found) {
            System.out.println("未找到符合条件的成绩信息！");
        }
    }

    public void statisticsGrade(String courseName) {
        List<Integer> scores = new ArrayList<>();
        for (StudentGrade grade : grades) {
            if (grade.getCourseName().equals(courseName)) {
                scores.add(grade.getGrade());
            }
        }

        if (scores.isEmpty()) {
            System.out.println("未找到该课程的成绩信息！");
            return;
        }

        double sum = 0;
        int max = scores.get(0);
        int min = scores.get(0);

        for (int score : scores) {
            sum += score;
            if (score > max) {
                max = score;
            }
            if (score < min) {
                min = score;
            }
        }

        double average = sum / scores.size();

        System.out.println("课程：" + courseName);
        System.out.printf("平均分：%.2f\n", average);
        System.out.println("最高分：" + max);
        System.out.println("最低分：" + min);
    }
}
